/**
 * @(#) ReqDExpenseMediator.as
 *
 * @date		2009. 02. 17.
 * @version		1.0 ?
 * @author		Shin, Yunwoo
 * @description	제증명신청 - Mediator
 * 				제증명 발행을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.holotreq.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.RequestApprovalMediator;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.holotreq.model.DExpenseApprovalProxy;
	import com.skt.common_hr.holotreq.model.HolOtReqProxy;
	import com.skt.common_hr.holotreq.model.vo.ReqDExpenseVO;
	import com.skt.common_hr.holotreq.view.components.ReqHolOtReq;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class ReqHolOtReqMediator extends CommonMediator
	{
		public static const NAME:String = "ReqHolOtReqMediator";

		public static const EXEC_NEW_REQUEST:String = "execNewRequest";
		public static const EXEC_MODI_REQUEST:String = "execModifyRequest";
		public static const EXEC_READ_REQUEST:String = "execReadRequest";
		public static const CLOSED_REQUEST_VIEW:String = "closedRequestView";

		public var key:String = "";

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function ReqHolOtReqMediator(viewComponent:Object)
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			setEventListener();
 		}

 		private function initView():void
 		{
 			key = "";
			view.changeMode(ReqHolOtReq.MODE_MODIFY);			
			view.txtComment.text = "";
			view.appComment = "";

 			var proxy:HolOtReqProxy = facade.retrieveProxy(HolOtReqProxy.NAME) as HolOtReqProxy;
 			proxy.workTypeCode();
 			
/* 			var listProxy:DExpenseListProxy = facade.retrieveProxy(DExpenseListProxy.NAME) as DExpenseListProxy;
			var start:Date = new Date(listProxy.BEGDA);
			var end:Date = new Date(listProxy.ENDDA);
			start.date--;
			end.date++;
			view.dfDinner.disabledRanges = [{rangeEnd:start}, {rangeStart:end}]; */
 		}

 		private function setEventListener():void
 		{
			view.addEventListener(ReqHolOtReq.CLICK_OK, onClickOK);
			view.addEventListener(ReqHolOtReq.CLICK_CANCEL, onClickCancel);
 		}

 		private function onClickOK(e:Event):void
 		{
 			save();
 		}

 		private function onClickCancel(e:Event):void
 		{
			/* silver  		
			if(key!="" && view.currentMode == ReqHolOtReq.MODE_MODIFY) {
 				delTempRequest(key);
 			}
 			else {
 				closeView();
 			} */
 			
 			closeView();
 		}
		
		public var selectedDate:Date;
 		public function closeView():void
 		{
 			selectedDate = view.selectedDate;
 			PopUpManager.removePopUp(view);
 			facade.sendNotification(CLOSED_REQUEST_VIEW, this);
 		}

 		private function loadDetailData(key:String):void
 		{
 			var proxy:HolOtReqProxy = facade.retrieveProxy(HolOtReqProxy.NAME) as HolOtReqProxy;
 			this.key = key;
 			proxy.load(key);
 		}

 		private function delTempRequest(key:String):void
 		{
 			var proxy:HolOtReqProxy = facade.retrieveProxy(HolOtReqProxy.NAME) as HolOtReqProxy;
 			proxy.del(key);
 		}

 		private var _savingData:Object;

 		private function save():void
 		{
 			var proxy:HolOtReqProxy = facade.retrieveProxy(HolOtReqProxy.NAME) as HolOtReqProxy;

 			_savingData = new Object();

 			_savingData.I_PERNR = Variables.myPernr;
 			_savingData.I_DATUM = DateUtil.toBasicString(view.selectedDate); 	// 일자
 			_savingData.I_WTYPE = view.cbWorkType.selectedItem.CODE; 			// 근무유형
 			_savingData.I_WWHAT = view.txtComment.text;							// 근무사유
 			_savingData.I_WSTAT = "1";							// 미지시

 			proxy.save(_savingData);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					EXEC_NEW_REQUEST,
					EXEC_READ_REQUEST,
					EXEC_MODI_REQUEST,
					HolOtReqProxy.LOAD_DINNER_COMPLETE,
					HolOtReqProxy.LOAD_DINNER_FAILED,
					HolOtReqProxy.SAVE_DINNER_COMPLETE,
					HolOtReqProxy.SAVE_DINNER_FAILED,
					HolOtReqProxy.DEL_DINNER_COMPLETE,
					RequestApprovalMediator.APPROVAL_CANCEL,
					RequestApprovalMediator.APPROVAL_COMPLETE, 
					HolOtReqProxy.LOAD_WORKTYPECODE_COMPLETE,
					HolOtReqProxy.LOAD_WORKTYPECODE_FAILED
					
					];
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName())
			{
				case HolOtReqProxy.LOAD_WORKTYPECODE_COMPLETE:
					var o:HolOtReqProxy = (note.getBody() as HolOtReqProxy);
					view.cbWorkType.dataProvider = o.acWorkType;
					break;
				case HolOtReqProxy.LOAD_WORKTYPECODE_FAILED:
					trace("ReqHolOtReqMediator.as LOAD_WORKTYPECODE_FAILED");
					break;					
				case EXEC_NEW_REQUEST:
					initView();
					break;
				case EXEC_MODI_REQUEST:
					initView();
					loadDetailData(note.getBody() as String);
					break;
				case EXEC_READ_REQUEST:
					view.changeMode(ReqHolOtReq.MODE_READONLY1);
					loadDetailData(note.getBody() as String);
					break;
				case HolOtReqProxy.LOAD_DINNER_COMPLETE:
					onLoadDinner(HolOtReqProxy(note.getBody()));
					break;
				case HolOtReqProxy.SAVE_DINNER_COMPLETE:
					onSavedDinner(HolOtReqProxy(note.getBody()));
					break;
				case HolOtReqProxy.DEL_DINNER_COMPLETE:
					closeView();
					break;
				case HolOtReqProxy.LOAD_DINNER_FAILED:
				
				case HolOtReqProxy.SAVE_DINNER_FAILED:
					CommonAlert.showMessage("", "("+ HolOtReqProxy(note.getBody()).retCode + ") " +HolOtReqProxy(note.getBody()).retText);
					break;
				case RequestApprovalMediator.APPROVAL_COMPLETE:
					closeView();
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}

		/**
		 * Main Module에서 QueryClose 메세지가 왔을 때의 처리
		 *     - 사용자에게 확인하고 View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 * @param
		 * @return void
		 */
		private var loadedDP:ReqDExpenseVO = null;
		private function onLoadDinner(proxy:HolOtReqProxy):void
		{
			loadedDP = proxy.dp;
			view.selectedDate = loadedDP.BEGDA;
			view.txtComment.text = loadedDP.TEXT1;
			view.appComment = loadedDP.TEXT2;

			if(view.currentMode!=ReqHolOtReq.MODE_MODIFY) {
				if(StringUtil.NVL(view.appComment) == "") {
					view.changeMode(ReqHolOtReq.MODE_READONLY2);
				}
			}
		}

		private function onSavedDinner(proxy:HolOtReqProxy):void
		{
			key = proxy.key;
			execApproval(proxy.key);
		}

 		private function execApproval(key:String):void
 		{

 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "10");
 				facade.registerMediator(approval);
 			}

 			var appProxy:DExpenseApprovalProxy = facade.retrieveProxy(DExpenseApprovalProxy.NAME) as DExpenseApprovalProxy;
 			_savingData.GWAREKEY = appProxy.key = key;
 			_savingData.DAY = view.selectedDate.day;
 			appProxy.dataProvider = _savingData;
 			approval.execRequest(view.parentApplication as DisplayObject, appProxy);
 		}

 		private function cancelApproval(key:String):void
 		{
 			//var proxy:ReqVacationProxy = facade.retrieveProxy(ReqVacationProxy.NAME) as ReqVacationProxy;
 			//proxy.del([{GWAREKEY: key}]);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqHolOtReq
        {
            return viewComponent as ReqHolOtReq;
        }


	}
}